package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.CreditTerm;
import org.hgvc.webapp.ets.bo.MembershipCategory;
import org.hgvc.webapp.ets.bo.ReInstateFreeTrx;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminMembershipCategoryBean extends AbstractBean {

	public static final String BEAN_NAME="adminMembershipCategoryBean";
	public static final String NAV_LIST="adminMemCatList";
	public static final String NAV_CREATE="adminMemCatCreate";
	public static final String NAV_EDIT="adminMemCatEdit";
	
	private MembershipService service;
	private MembershipCategory category;
	private List<MembershipCategory> categoryList;
	private List<SelectItem> termList;
	private List<SelectItem> resList;
	private List<SelectItem> yeNoList;
	private String termId;
	private String resId;
	private String trans;
	private boolean check;
	
	public MembershipCategory getCategory() {
		return category;
	}
	public void setCategory(MembershipCategory category) {
		this.category = category;
	}

	public void setService(MembershipService service) {
		this.service = service;
	}
	public String getTermId() {
		return termId;
	}
	public void setTermId(String termId) {
		this.termId = termId;
	}
	public String getResId() {
		return resId;
	}
	public void setResId(String resId) {
		this.resId = resId;
	}
	public String getTrans() {
		return trans;
	}
	public boolean isCheck() {
		return check;
	}
	public void setCheck(boolean check) {
		this.check = check;
	}
	/*------------------------------------------------------------*/
	public String navListAction(){
		return NAV_LIST;
	}
	
	public List<MembershipCategory> getCategoryList() {
		try{
			categoryList = service.getMembershipCategoryList();
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return categoryList;
	}
	
	public String navCreateAction(){
		category = new MembershipCategory();
		check = true;
		trans = CommonConstant.SELECT_YES;
		return NAV_CREATE;
	}
	
	public String createAction(){
		try{
			category.setReInstateFreeTrx(new ReInstateFreeTrx(resId));
			category.setCreditTerm(new CreditTerm(termId));
			category.setTransferable(trans);
			service.createMembershipCategory(category, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_CREATE;
	}
	
	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("catId");
		String id = (String)param.getValue();
		for(MembershipCategory cat:categoryList){
			if(cat.getMembershipCatId().equalsIgnoreCase(id)){
				category = cat;
				resId = category.getReInstateFreeTrx().getReInstateFreeTrxId();
				termId = category.getCreditTerm().getCreditTermId();
				trans = category.getTransferable();
				if(trans.equalsIgnoreCase(CommonConstant.SELECT_YES)){
					check = true;
				}else{
					check = false;
				}
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT;
	}
	
	public String editAction(){
		try{
			category.setReInstateFreeTrx(new ReInstateFreeTrx(resId));
			category.setCreditTerm(new CreditTerm(termId));
			category.setTransferable(trans);
			service.editMembershipCategory(category, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_EDIT;
	}
	
	public String deleteAction(){
		try{
			service.deleteMembershipCategory(categoryList, getThisUser());
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_LIST;
	}
	public List<SelectItem> getTermList() {
		try{
			termList = new ArrayList<SelectItem>();
			List<CreditTerm> list = service.getCreditTermList();
			for(CreditTerm term:list){
				SelectItem item = new SelectItem(term.getCreditTermId(),term.getCode()+" "+term.getName());
				termList.add(item);
			}
			
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return termList;
	}
	public List<SelectItem> getResList() {
		try{
			resList = new ArrayList<SelectItem>();
			List<ReInstateFreeTrx> list = service.getReInstateFreeTrxList();
			for(ReInstateFreeTrx res:list){
				SelectItem item = new SelectItem(res.getReInstateFreeTrxId(),res.getCode()+" "+res.getName());
				resList.add(item);
			}
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return resList;
	}
	public List<SelectItem> getYeNoList() {
		yeNoList= new ArrayList<SelectItem>();
		for(String [] array:CommonConstant.yesnoSelectList){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1], new Object[]{}));
			yeNoList.add(item);
		}
		return yeNoList;
	}
	public void setTrans(String trans) {
		this.trans = trans;
		if(trans.equalsIgnoreCase(CommonConstant.SELECT_YES)){
			check = true;
		}else{
			check = false;
		}
	}
	
}
